package com.erp.module.YuliaoPrint.DayinYuliaoActivity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.Myapp
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.YuliaoPrintBean
import com.erp.module.YuliaoPrint.FragmengtYuliaoPrintLabel
import com.erp.module.YuliaoPrint.YuliaoPrintContract
import com.erp.myAdapter.ArrayAdapter
import com.erp.util.Dialogutil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.example.tsc.tscdll_test.TSCActivity
import kotlinx.android.synthetic.main.activity_yuliao_dayin.*
import kotlinx.android.synthetic.main.header_knoya.*
import kotlin.concurrent.fixedRateTimer

class DayinYuliaoActivity : BaseActivity(),YuliaoPrintContract.DayingYuliaoView, View.OnClickListener {

    var updatedialog: AlertDialog? = null
    var uids: MutableList<String> = java.util.ArrayList()
    var names: MutableList<String> = java.util.ArrayList()
//    var mid = "";
//    var kailiao_uid = "";
//    var kailiao_name = "";
//    var time = "";
//    var position = ""
    lateinit var P: DayinYuliaoP
    lateinit var bean : YuliaoPrintBean
    override fun getLayoutId(): Int {
        return R.layout.activity_yuliao_dayin
    }

    override fun initCreate() {

        P = DayinYuliaoP(this,this)
        bean = FragmengtYuliaoPrintLabel.bean

//        mid = intent.getStringExtra("mid")
//        position = intent.getStringExtra("position")
//        time = intent.getStringExtra("kailiao_time")
//        kailiao_uid = intent.getStringExtra("kailiao_wu_id")
//        kailiao_name = intent.getStringExtra("kailiao_name")
        tv_header_title.text = "打印标签"
        iv_header_left.setOnClickListener(this)
        iv_header_left.visibility = View.VISIBLE
        BtQueding.setOnClickListener(this)
        AtGongHao.setOnClickListener { AtGongHao.showDropDown() }
        TvTime.setOnClickListener(this)
        EtPosition.setText(bean.position)
        if(bean.kailiao_time == ""){
            TvTime.text = Myutil.getjulidate(0)
        }else{
            TvTime.text = Myutil.getdata(bean.kailiao_time)
        }
        AtGongHao.setText(bean.kailiao_name)
        P.getwage_user()
    }

    override fun getwage_userOk(uids: MutableList<String>, names: MutableList<String>) {
        this.uids = uids
        this.names = names
        val av = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, this.names)
        AtGongHao.setAdapter(av)
    }

    override fun onClick(p0: View?) {
        when(p0){
            iv_header_left -> {
                finish()
            }
            TvTime -> {
                Dialogutil.showBottoPopupWindow(TvTime, "选择开料时间", TvTime.text.toString(), this)
            }
            BtQueding -> {
                var uid = ""
                if (names.contains(AtGongHao.text.toString())) {
                    names.indices
                            .filter { names[it] == AtGongHao.text.toString() }
                            .forEach { uid = uids[it] }
                }
                var time = TvTime.text.toString()
                var position = EtPosition.text.toString()
                if(position ==""){
                    Myutil.MyTost("必须填写仓位号")
                    return
                }
                if(uid == ""){
                    Myutil.MyTost("必须选择开料员工")
                    return
                }
                updatedialog = Myutil.showupdatedialog(this, "正在上传标记", false)
                P.postBiaoshi(bean.mid,position,time,uid)
            }
        }
    }


    override fun postBiaoshiNo() {
        updatedialog!!.dismiss()
    }

    override fun postBiaoshiOk(msg:String) {
        updatedialog!!.dismiss()
        Myutil.MyTost(msg)
        val handle = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message?) {
                msg.let {
                    updatedialog?.dismiss()
                }
            }
        }
        updatedialog = Myutil.showupdatedialog(this, "正在打印标签", false)
        val thread = object : Thread() {
            override fun run() {

                dayin(bean,handle)
            }
        }
        thread.start()
    }

    override fun getwage_userNo() {
    }



    fun dayin(bean : YuliaoPrintBean,handler: Handler){

        var tscActivity = TSCActivity()

        tscActivity.openport(FragmengtYuliaoPrintLabel.lanya);
        tscActivity.setup(110, 40, 3, 4, 0, 2, 0);
        tscActivity.clearbuffer();

        tscActivity.sendcommand("PUTBMP 10,10,\"logtsc.BMP\"")

        tscActivity.sendcommand("\n");
        tscActivity.sendcommand("QRCODE " + 10 + "," + 80 + ",H,7,A,0,M2,S7,\"" + bean.mcode + "\"");
        tscActivity.sendcommand("\n");

        tscActivity.sendcommand("QRCODE " + 950 + "," + 200 + ",H,7,A,0,M2,S7,\"" + bean.mcode+","+bean.color_code + "\"");
        tscActivity.sendcommand("\n");
        tscActivity.sendcommand("PUTBMP 800,150,\"yuanyuan.BMP\"")
        tscActivity.sendcommand("\n");

        val typeface = Typeface.createFromAsset(Myapp.getContext().assets, "宋体-粗体.ttf")

        tscActivity.windowsfont(250, 20, 0, 60, typeface, bean.m_name);

        tscActivity.windowsfont(600, 20, 0, 60, typeface, bean.mb_batch+",")

        tscActivity.windowsfont(1000, 20, 0, 60, typeface, bean.mb_area_num );

        tscActivity.windowsfont(250, 80, 0, 50, typeface, "开料：" + bean.m_size)

        tscActivity.windowsfont(750, 80, 0, 50, typeface, "成品："+bean.finished_size)

        tscActivity.windowsfont(250, 150, 0, 40, typeface, "颜色："+bean.color_color)

        tscActivity.windowsfont(250, 220, 0, 50, typeface, "封边："+bean.es_type+",")

        tscActivity.windowsfont(815, 185, 0, 50, typeface, bean.ban_jian_hao)

        tscActivity.windowsfont(20, 280, 0, 40, typeface, Myutil.getdata1(bean.mb_productive_time));

        tscActivity.windowsfont(400, 280, 0, 50, typeface, bean.mm_menjiaokong)




        tscActivity.windowsfont(20, 340, 0, 50, typeface, "备注："+bean.m_remark)

        tscActivity.windowsfont(20, 410, 0, 50, typeface, "开槽："+bean.slot_name)
        tscActivity.windowsfont(370, 410, 0, 50, typeface, bean.weihao+",")
        tscActivity.windowsfont(460, 410, 0, 50, typeface, bean.mcode+",")
        tscActivity.windowsfont(1100, 410, 0, 50, typeface, bean.es_dnum_mark+",")


        Thread.sleep(2000);
        tscActivity.printlabel(1, bean.m_counts.toInt());
        tscActivity.closeport()
        val msg = Message.obtain()
        msg.obj = "信息"
        handler.sendMessage(msg)
    }

}